package com.nwork.qingmanhua.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nwork.qingmanhua.MainActivity;
import com.nwork.qingmanhua.R;
import com.nwork.qingmanhua.adapter.CategoryImageAdapter;
import com.nwork.qingmanhua.model.CategoryModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类的Fragment
 */
public class CategoryFragment extends Fragment {

    private List<String> list;
    private RecyclerView recyclerView;
    private TextView txtFlag;
    private int topLayoutHeight;
    private String jsonCategory;
    private List<CategoryModel> categoryModelList;

    public CategoryFragment () {
    }

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycle_fragment_category);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        txtFlag = (TextView) view.findViewById(R.id.flag_fragment_category);
        list = new ArrayList<>();
        try {
            String[] strings = getActivity().getAssets().list("");
            for (String s : strings) {
                list.add(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        FragmentActivity fragmentActivity = getActivity();
        if (fragmentActivity instanceof MainActivity) {
            MainActivity activity = (MainActivity) fragmentActivity;
            topLayoutHeight = activity.getTopLayoutHeight();
        }
        ViewGroup.LayoutParams layoutParams = txtFlag.getLayoutParams();
        layoutParams.height = topLayoutHeight;
        txtFlag.setLayoutParams(layoutParams);
        Log.d("++++", "list的长度 = " + list.size());
        recyclerView.setAdapter(new CategoryImageAdapter(getActivity(), list));
        return view;
    }


}